<!--
 * @Description: 动态组件
 * @Author: zzj
 * @Date: 2021-10-13 18:44:56
 * @LastEditors: zzj
 * @LastEditTime: 2021-10-13 19:02:16
-->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>组件</title>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
</head>
<body>
	<div id="app">
		<button @click="current='briup-article'">文章</button>
		<button @click="current='briup-category'">栏目</button>
		<!-- 使用keep-alive组件，可以保持栏目的状态，不需要在切换时销毁创建 -->
		<keep-alive>
			<component :is="current"></component>
		</keep-alive>
	</div>
	<script>
		Vue.component("briup-article",{
			template:`
				<div>
					文章管理	
				</div>
			`
		});
		Vue.component("briup-category", {
				template: `
				<div>
					栏目管理	
				</div>
			`,
			 //在组件切换的时候，组件会重新加载（切入创建，切出销毁），使用keep-alive可以避免此问题
			mounted(){ 
				alert("category mounted");
			},
			beforeDestroy(){
				alert("category beforeDestroy");
			}
			});
		new Vue({
			el:"#app",
			data:{
				current: "briup-article"   //默认页面
			}
		})
	</script>
</body>
</html>